Ext.onReady(function() {
	var required = '<span style="color:red;font-weight:bold" data-qtip="Required">*</span>';
	var id = 0;
	var qryParams = Ext.Object.fromQueryString(location.search);
	if(!Ext.isEmpty(qryParams) && !Ext.isEmpty(qryParams.id)){
		id = qryParams.id;
	}

	
	var toolbar = Ext.create('Ext.toolbar.Toolbar', {
		dock : 'top',
		items : [{
					text : '返回',
					xtype : 'button',
					listeners : {
						click : function() {
							location.href = "/wuziMgr/sb_ys";
						}
					}
				}, {
					text : '添加',
					xtype : 'button',
					listeners : {
						click : function() {
							var form = Ext.getCmp("device_acceptance");
							if (!form.isValid()) {
								return;
							}
							if(Ext.getCmp('state').getValue() == 1 && Ext.isEmpty(Ext.getCmp('reason').getValue()))
							{
								Ext.Msg.alert('错误', '验收未通过原因未填写！');
								return;
							}
							var params = {
									Id : id,
									Device : Ext.getCmp('device').getValue(),
									State : Ext.getCmp('state').getValue(),
									Reason : Ext.getCmp('reason').getValue(),
									Time : Ext.Date.format(Ext.getCmp("ysTime")
											.getValue(), "Y-m-d H:i:s"),
							};		
							
							Ext.Ajax.request({
								url : "/device_acceptance/add",
								type : "POST",
								params : params,
								callback : function(options, success, response) {
									if (success) {
										var result = Ext.decode(response.responseText);
										if (result.success) {
											location.href = "/wuziMgr/sb_ys";
										} else {
											Ext.Msg.alert("错误", result.msg);
										}
									}
								}
							})
						}
					}
				}]
	});

	var deviceStore = Ext.create('Ext.data.Store', {
				fields : [{
							name : 'Id',
							type : 'int'
						}, 'No'],
				proxy : {
					type : 'ajax',
					url : '/device/all',
					reader : {
						type : 'json',
						root : 'data.rows'
					}
				},
				autoLoad : true
			});

	var device = Ext.create('Ext.form.ComboBox', {
				store : deviceStore,
				id : "device",
				editable : false,
				fieldLabel : "设备",
				displayField : 'No',
				valueField : 'Id',
				labelWidth: 80,
				allowBlank: false,
				afterLabelTextTpl: required,
				listeners : {
					afterRender : function(combo) {
					}
				}
			});
	var stateStore = Ext.create('Ext.data.Store', {
		fields : ['display', 'value'],
		data : [{
					"display" : "已验收",
					"value" : 0
				}, {
					"display" : "未通过",
					"value" : 1
				}]
	});
	
	var state = Ext.create('Ext.form.ComboBox', {
		fieldLabel : '状态',
		labelWidth: 80,
		editable : false,
		queryMode : 'local',
		displayField : 'display',
		valueField : 'value',
		allowBlank: false,
		afterLabelTextTpl: required,
		id : 'state',
		store : stateStore,
		listeners : {
			change: function(self, newValue, oldValue){
				var reason = Ext.getCmp('reason');
				reason.setDisabled(newValue == 1 ? false : true);
				
				if(newValue == 0)
				{
					reason.setValue('');
				}
			}
		}
	});
	
	var detailPanel = Ext.create("Ext.form.Panel", {
				border : false,
				autoScroll : true,
				margin : '5 5',
				id : 'device_acceptance',
				items : [device, {
					xtype : 'datefield',
					labelWidth: 80,
					fieldLabel : '验收时间',
					format : 'Y-m-d',
					id : 'ysTime',
					allowBlank: false,
					afterLabelTextTpl: required
				}, state, {
					xtype : 'textarea',
					width : 400,
					id : "reason",
					labelWidth: 80,
					disabled: true,
					fieldLabel : '未通过原因'
				}]
			});

	var panel = Ext.create("Ext.panel.Panel", {
				title : '',
				border : false,
				bodyBorder : false,
				autoScroll : true,
				layout : 'fit',
				dockedItems : [toolbar],
				items : [detailPanel],
				listeners : {
					'afterrender' : function()
					{
						if(!Ext.isEmpty(deviceAcceptance))
						{
							id = deviceAcceptance.Id;
							Ext.getCmp('device').setValue(deviceAcceptance.Device.Id);
							Ext.getCmp('state').setValue(deviceAcceptance.State);
							Ext.getCmp('reason').setValue(deviceAcceptance.Reason);
							Ext.getCmp('ysTime').setValue(Ext.Date.parse(deviceAcceptance.AddTime, "Y-m-d H:i:s"));
						}
					}
				}
			});
	Ext.create('Ext.Viewport', {
				layout : 'fit',
				style : 'background:none;background-color:white;',
				items : [panel]
			});
});